package nc.levm.common.utils;

import java.security.MessageDigest;

public class MD5Utils {
	

    public static final String ALGORITHM_MD5 = "MD5";

    /**
     * MD5 ENCODE
     *
     * @param content
     * @return base64ToString
     */
//    public static String encodeBase64(String content) {
//
//        if (StringUtils.isEmpty(content)) {
//            return null;
//        }
//
//        try {
//            return Base64Utils.encodeToString(MessageDigest.getInstance(ALGORITHM_MD5).digest(content.getBytes(StandardCharsets.UTF_8)));
//        } catch (NoSuchAlgorithmException e) {
//            logger.error("", e);
//        }
//        return null;
//    }


    private static final String toHex(byte hash[])
    {
        if (hash == null)
        {
            return null;
        }
        StringBuffer buf = new StringBuffer(hash.length * 2);
        int i;

        for (i = 0; i < hash.length; i++)
        {
            if ((hash[i] & 0xff) < 0x10)
            {
                buf.append("0");
            }
            buf.append(Long.toString(hash[i] & 0xff, 16));
        }
        return buf.toString();
    }

    public static String hash(String s)
    {
        try
        {
            return new String(toHex(MessageDigest.getInstance(ALGORITHM_MD5).digest(s.getBytes("UTF-8"))).getBytes("UTF-8"), "UTF-8");
        }
        catch (Exception e)
        {
            return s;
        }
    }
}
